package com.piece.mall.service;

import com.piece.core.jpa.repository.BaseService;
import com.piece.mall.model.SmsSeckillSkuRelation;
import com.piece.mall.repository.SmsSeckillSkuRelationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;

@Service
@Transactional
public class SmsSeckillSkuRelationService extends BaseService<SmsSeckillSkuRelation, String> {

    @Autowired
    private SmsSeckillSkuRelationRepository smsSeckillSkuRelationRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    public void initRepository() {
        setRepository(smsSeckillSkuRelationRepository);
    }

    /**
     * 获取下一个序号
     */
    public Long findNextSeckillSort(Long sessionId) {
        String sql = "select ifnull(max(seckill_sort) + 1,1) from sms_seckill_sku_relation where session_id = " + sessionId ;
        return jdbcTemplate.queryForObject(sql, Long.class);
    }
}
